//
//  ModuleViewController.m
//  Game2048
//
//  Created by mis on 16/3/24.
//  Copyright © 2016年 mis. All rights reserved.
//

#import "ModuleViewController.h"
#import "ModuleView.h"
#import "ModuleModel.h"


@interface ModuleViewController () <ModuleDelegate>
{
    UInt16 (*moduleData)[4];
}
@property (nonatomic, strong) ModuleView *moduleView;
@property (nonatomic, strong) ModuleModel *moduleModel;
@property (nonatomic, strong) UISwipeGestureRecognizer *swipUp;
@property (nonatomic, strong) UISwipeGestureRecognizer *swipDown;
@property (nonatomic, strong) UISwipeGestureRecognizer *swipLeft;
@property (nonatomic, strong) UISwipeGestureRecognizer *swipRight;
@end

@implementation ModuleViewController


- (UISwipeGestureRecognizer *)swipUp {
    if (!_swipUp) {
        _swipUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
        _swipUp.direction = UISwipeGestureRecognizerDirectionUp;
    }
    return _swipUp;
}

- (UISwipeGestureRecognizer *)swipDown {
    if (!_swipDown) {
        _swipDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
        _swipDown.direction = UISwipeGestureRecognizerDirectionDown;
    }
    return _swipDown;
}

- (UISwipeGestureRecognizer *)swipLeft {
    if (!_swipLeft) {
        _swipLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
        _swipLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    }
    return _swipLeft;
}

- (UISwipeGestureRecognizer *)swipRight {
    if (!_swipRight) {
        _swipRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
        _swipRight.direction = UISwipeGestureRecognizerDirectionRight;
    }
    return _swipRight;
}

- (ModuleView *)moduleView {
    if (!_moduleView) {
        _moduleView = [[ModuleView alloc] initWithFrame:CGRectMake(0, 60 + (self.view.frame.size.height - self.view.frame.size.width - 160) * 0.5, self.view.frame.size.width, self.view.frame.size.width)];
        _moduleView.backgroundColor = [UIColor clearColor];
        
//        moduleData[1][1] = 2;
//        UInt16 data = 2;
//        for (UInt8 row = 0; row < 4; row++) {
//            for (UInt8 col = 0; col < 4; col++) {
//                moduleData[row][col] = data;
//                data *= 2;
//            }
//        }
        _moduleView.moduleData = moduleData;
    }
    return _moduleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.moduleModel = [ModuleModel new];
    moduleData = [self.moduleModel loadModuleData];
    self.moduleModel.delegate = self;
    [self.view addSubview:self.moduleView];
    [self.moduleView addGestureRecognizer:self.swipUp];
    [self.moduleView addGestureRecognizer:self.swipDown];
    [self.moduleView addGestureRecognizer:self.swipRight];
    [self.moduleView addGestureRecognizer:self.swipLeft];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 重写了父类中的方法 覆盖了父类的方法
- (void)refreshBtnClick {
    NSLog(@"ModuleViewController refreshBtnClick");
//    for (UInt8 row = 0; row < 4; row++) {
//        for (UInt8 col = 0; col < 4; col++) {
//            moduleData[row][col] = 0;
//        }
//    }
    // 把一段内存清零，自己调用自己负责
    bzero(moduleData, sizeof(moduleData)); // 代替上面的代码
    [self.moduleView setNeedsDisplay];
}

- (void)moduleMoveDestRow:(UInt8)destRow DestCol:(UInt8)destCol SrcRow:(UInt8)srcRow SrcCol:(UInt8)srcCol {
    UILabel *destModule = self.moduleView.moduleArray[destRow][destCol];
    UILabel *srcModule = self.moduleView.moduleArray[srcRow][srcCol];
    
    CGRect destFrame = destModule.frame;
    CGRect srcFrame = srcModule.frame;
    [UIView animateWithDuration:0.1 animations:^{
        srcModule.frame = destFrame;
    }];
    
    destModule.frame = srcFrame;
    self.moduleView.moduleArray[destRow][destCol] = srcModule;
    self.moduleView.moduleArray[srcRow][srcCol] = destModule;
}

- (void)moduleZoomRow:(UInt8)row Col:(UInt8)col {
    UILabel *module = self.moduleView.moduleArray[row][col];
    module.backgroundColor = [self.moduleView getModuleColor:2];
    module.text = @"2";
    module.transform = CGAffineTransformMakeScale(0.5, 0.5);
    [UIView animateWithDuration:0.2 animations:^{
        module.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
}

- (void)swipAction:(UISwipeGestureRecognizer *)swip {
    switch (swip.direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"up");
            [self.moduleModel moduleUp];
            break;
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"right");
            [self.moduleModel moduleRight];
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"left");
            [self.moduleModel moduleLeft];
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"down");
            [self.moduleModel moduleDown];
            break;
    }
    [self.moduleView setNeedsDisplay];
}

@end
